<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:TestFilter.php,2010/05/31 16:52:02 
 * 
 **************************************************************************/
 
 
 
/**
 * @file TestFilter.php
 * @author huqingping(huqingping@)
 * @date 2010/05/31 16:52:02
 * @version 1.0 
 * @brief 
 *  
 **/

require_once dirname(__FILE__).'/../IFilter.php';
require_once dirname(__FILE__).'/../hapn.php';
require_once dirname(__FILE__).'/../WebApp.php';

class TestFilter extends TestCase
{
	function init()
	{
		$url = '/test';
		$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
		$_SERVER['REQUEST_METHOD'] = 'POST';
		$_SERVER['SCRIPT_URL'] = $url; 
		$_SERVER['REQUEST_URI'] = $url; 
		$app = new MCWebApp();
		$app->init();
		return $app;
	}

	function testYouaInit()
	{
		require_once FILTER_ROOT.'YouaInitFilter.php';
		$app = $this->init();
		$filter = new YouaInitFilter();
		$filter->execute($app);

		$app->request->inputs = array('id'=>'asd','xid'=>12,'cid'=>'aa', 'X'=>'DD');
		$filter->execute($app);
		$this->assertEquals('asd',$app->request->inputs['id']);
		$this->assertEquals(12,$app->request->inputs['xid']);
		$this->assertEquals('aa',$app->request->inputs['cid']);
		$this->assertEquals('DD',$app->request->inputs['X']);


		$app->request->inputs = array('id'=>'0','xid'=>12,'cid'=>'aa', 'X'=>'DD');
		$filter->execute($app);
		$this->assertEquals('0',$app->request->inputs['id']);
		$this->assertEquals(12,$app->request->inputs['xid']);
		$this->assertEquals('aa',$app->request->inputs['cid']);
		$this->assertEquals('DD',$app->request->inputs['X']);

		$app->request->inputs = array('id'=>'5e40303d10436422437f4781','xid'=>'5e40303d10436422437f478112','cid'=>'', 'X'=>'DD');
		$filter->execute($app);
		$this->assertEquals(31282,$app->request->inputs['id']);
		$this->assertEquals('5e40303d10436422437f478112',$app->request->inputs['xid']);
		$this->assertEquals('',$app->request->inputs['cid']);
		$this->assertEquals('DD',$app->request->inputs['X']);

		$app->request->inputs = array('id'=>'5e40303d10436422437f4781','cid'=>'', 'X'=>'DD');
		$filter->execute($app);
		$this->assertEquals(31282,$app->request->inputs['id']);
		$this->assertEquals('',$app->request->inputs['cid']);
	}

	function testYouaInputFilter()
	{
		require_once FILTER_ROOT.'YouaInputFilter.php';
		$app = $this->init();
		$filter = new YouaInputFilter();
	}

	function testYouaOutputFilter()
	{
		require_once FILTER_ROOT.'YouaOutputFilter.php';
		$app = $this->init();
		$filter = new YouaOutputFilter();

		$app->response->outputs = array('id'=>'asd','xid'=>12,'cid'=>'aa', 'X'=>'DD');
		$filter->execute($app);
		$this->assertEquals('asd',$app->response->outputs['id']);
		$this->assertEquals(12,$app->response->outputs['xid']);

		$app->response->outputs = array('id'=>31282,'cid'=>'aa', 'X'=>'DD');
		$filter->execute($app);
		$this->assertEquals('5e40303d10436422437f4781',$app->response->outputs['id']);
		$this->assertEquals('aa',$app->response->outputs['cid']);

		$app->response->outputs = array('id'=>'31282,31282','d'=>array('id'=>31282), 'X'=>'DD');
		$filter->execute($app);
		$this->assertEquals('5e40303d10436422437f4781,5e40303d10436422437f4781',$app->response->outputs['id']);
		$this->assertEquals('5e40303d10436422437f4781',$app->response->outputs['d']['id']);
	}

	function testIPAuth()
	{
		require_once FR_ROOT.'filters/IPAuthFilter.php';
		$app = $this->init();
		$app->request->isPrivate = true;
		Conf::set('hapn.privateip',array('10.23','192.11'));
		$filter = new IPAuthFilter();
		$app->request->clientip = '127.0.0.1';
		$filter->execute($app);
		$app->request->clientip = '10.23.3.4';
		$filter->execute($app);
		$app->request->clientip = '192.111.233.2';
		$filter->execute($app);
		$app->request->clientip = '192.11.233.2';
		$filter->execute($app);
		$app->request->clientip = '10.23.32.4';
		$filter->execute($app);
		try {
			$app->request->clientip = '223.22.0.1';
			$filter->execute($app);
			$this->fail('should exception');
		}catch(Exception $ex) {$this->assertEquals('hapn.ipauthfail',$ex->getMessage());}
		try {
			$app->request->clientip = '132.12.0.1';
			$filter->execute($app);
			$this->fail('should exception');
		}catch(Exception $ex) {$this->assertEquals('hapn.ipauthfail',$ex->getMessage());}
		try {
			$app->request->clientip = '131.12.0.1';
			$filter->execute($app);
			$this->fail('should exception');
		}catch(Exception $ex) {$this->assertEquals('hapn.ipauthfail',$ex->getMessage());}
	}

	function testSwitchMod()
	{
		require_once FR_ROOT.'filters/ModuleSwitchFilter.php';
		$app = $this->init();
		$filter = new ModuleSwitchFilter();

		$app->request->method = 'POST';
		$filter->execute($app);

		Conf::set('hapn.switch.post',false);
		try{
			$filter->execute($app);
			$this->fail('should exception');
		}catch(Exception $ex) {$this->assertEquals('hapn.u_power',$ex->getMessage());}

		Conf::set('hapn.switch.post',true);
		$app->request->url = '/item/1';
		$filter->execute($app);

		Conf::set('hapn.switch.disableapp', array('item'));
		try{
			$filter->execute($app);
			$this->fail('should exception');
		}catch(Exception $ex) {$this->assertTrue(strncmp('hapn.u_power',$ex->getMessage(),13) == 0);}
	}


	function testCSRF()
	{
		require_once FR_ROOT.'filters/CSRFFilter.php';
		$app = $this->init();
		$app->request->method = 'POST';
		$app->request->userData['_user'] = array('uid'=>1);
		$app->request->serverEnvs['HTTP_REFERER'] = 'youa';
		Conf::set('csrf.expires',2);
		$filter = new CSRFFilter();
		$app->request->url = '/_posttoken';
		ob_start();
		$filter->execute($app);
		$token = ob_get_contents();
		ob_end_clean();
		$app->request->url = '/commit';    
		$app->request->inputs['_posttoken'] = $token;
		$filter->execute($app);
		unset($app->request->userData['_user']);
		try{
			$filter->execute($app);
			$this->fail('empty user');
		}catch(Exception $ex){$this->assertEquals('hapn.u_login',$ex->getMessage());}

		//invalid user
		$app->request->userData['_user'] = array('uid'=>2); 
		$app->request->serverEnvs['HTTP_REFERER'] = 'youa'; 
		try{
			$filter->execute($app);
			$this->fail('check user fail');
		}catch(Exception $ex) {$this->assertEquals('hapn.u_notfound',$ex->getMessage());}
		//invalid refer
		$app->request->userData['_user'] = array('uid'=>1); 
		$app->request->serverEnvs['HTTP_REFERER'] = 'youa2'; 
		$app->request->url = '/commit';    
		try{
			$filter->execute($app);
			$this->fail('check refer fail');
		}catch(Exception $ex) {$this->assertEquals('hapn.u_notfound',$ex->getMessage());}
		//invalid time
		sleep(3);
		$app = $this->init();
		$app->request->now = time();
		$app->request->userData['_user'] = array('uid'=>1); 
		$app->request->method = 'POST';
		$app->request->serverEnvs['HTTP_REFERER'] = 'youa'; 
		$app->request->inputs['_posttoken'] = $token;
		$app->request->url = '/commit';    
		try{
			$filter->execute($app);
			$this->fail('check time fail');
		}catch(Exception $ex) {$this->assertEquals('hapn.u_notfound',$ex->getMessage());}

	}

	function testURLAccess()
	{
		//only test supers
		require_once FR_ROOT.'filters/URLAccessFilter.php';
		$app = $this->init();
		$app->request->url = '/testurl';
		$app->request->userData['_user'] = array('uid'=>1);
		$filter = new CSRFFilter();

		Conf::set('urlacl.supers',array('u1','u2'));
		try{
			$filter->execute($app);
		}catch(Exception $ex) {$this->assertEquals('hapn.u_login',$ex->getMessage());}
		$app->request->userData['_user'] = array('uid'=>1,'uname'=>'u3');
		try{
			$filter->execute($app);
		}catch(Exception $ex) {$this->assertEquals('hapn.u_power',$ex->getMessage());}
		$app->request->userData['_user'] = array('uid'=>1,'uname'=>'u2');
		$filter->execute($app);
		$app->request->userData['_user'] = array('uid'=>1,'uname'=>'u1');
		$filter->execute($app);
	}
}






/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
?>
